#pragma once
#include "Singleton.h"
#include "MsgRouter.h"
#include "expire_def.h"
#include <string>
#include <map>
#include <unordered_map>
#include <functional>

typedef std::function<void(std::uint32_t request_id, std::uint64_t request_time, std::int32_t errorCode, const std::string& message, const HttpSessionData& http_session) > ERRRESPCALLFUNC;
// http转发路由
struct ActionHttpRouteData
{
    std::string method;
    std::string path;
    std::uint32_t reqSrvType;
    std::uint32_t reqCmd;
    std::uint32_t rspCmd;
    std::uint32_t modUidType; // 详见 ServiceAllocType
    ERRRESPCALLFUNC errRespCallFunc;

    ActionHttpRouteData():
        reqSrvType(0),
        reqCmd(0), 
        rspCmd(0), 
        modUidType(0) 
    {

    }
};

struct HttpReqTempData{
    HttpSessionData http_session;
    std::uint64_t request_time = 0;
    std::uint64_t trans_id = 0;
    std::uint32_t request_id = 0;
};


class ActionHttpProxy
{
public:
    ActionHttpProxy();
    virtual ~ActionHttpProxy();

public:
    static ActionHttpProxy* Instance()
	{
		return CSingleton<ActionHttpProxy>::Instance();
	}


public:
    bool Init();
    void OnTick(std::int64_t tick);
    bool RegiserHttpRoute(const std::string& method, const std::string& path, std::uint32_t reqSrvType, std::uint32_t reqCmd, std::uint32_t rspCmd, std::uint32_t modUidType, ERRRESPCALLFUNC errRespCallFunc);
    void HttpHandle(HttpData&& data);
    void MsgHandle(MsgData&& data);

protected:
    void RemoveHttpReqTempData(std::uint32_t request_id);

private:
    // http 请求临时缓存
    std::unordered_map<std::uint32_t, HttpReqTempData> m_httpreq_info_map;
    std::map<std::uint64_t, std::uint32_t> m_expire_httpreq_map;

    std::map<std::string, ActionHttpRouteData> m_http_route;

    std::uint64_t m_trans_id = 0;
};

